/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2021-07-23 11:22:22
 * @LastEditors: voanit
 * @LastEditTime: 2021-08-05 17:44:44
 */
const GetUserState = () => {
  return {
    token: '', // Token信息
    nickName: '', // 用户昵称

    userName: '', // 用户账户	
    userPic: '', // 用户头像	
    userPk: '',// 用户主键

    userInfo: {} // 登录信息集合
  }
}
const state = new GetUserState();

const mutations = {
  // 初始化state
  RE_SET (state) {
    Object.assign(state, new GetUserState())
  },

  SET_TOKEN (state, data) {
    state.token = data;
  },
  SET_NICK_NAME (state, data) {
    state.nickName = data;
  },
  SET_USER_NAME (state, data) {
    state.userName = data;
  },
  SET_USER_PIC (state, data) {
    state.userPic = data;
  },
  SET_USER_PK (state, data) {
    state.userPk = data;
  },

  SET_USER_INFO (state, data) {
    state.userInfo = data;
  }
}

const actions = {
  setUserInfo ({ commit }, data) {
    commit('SET_TOKEN', data ? data.token || data.ticket : '');
    // 单独储存部分登录信息，方便开发使用
    commit('SET_NICK_NAME', data ? data.fullName : '');

    commit('SET_USER_NAME', data ? data.userName : '');
    commit('SET_USER_PIC', data ? data.userPic : '');
    commit('SET_USER_PK', data ? data._id : '');

    // 登录信息集合
    commit('SET_USER_INFO', data || '')
  },
  clearUserInfo ({ commit }) {
    commit('RE_SET');
  }
}

export default {
  state,
  mutations,
  actions
}